import { SAVE_USER, SAVE_ROUTES } from '../actions/user/actionType'

interface UserAction {
  type: typeof SAVE_USER | typeof SAVE_ROUTES
  payload: any
}

const initState = {
  userInfo: {},
  routes: []
}

const user = (state = initState, action: UserAction) => {
  const { type, payload } = action
  switch (type) {
    case SAVE_USER:
      const { permissions, roles, user } = payload
      return { ...state, userInfo: { permissions, roles, user } }
    case SAVE_ROUTES:
      return { ...state, routes: payload.routes }
    default:
      return state
  }
}

export default user